import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:bookapp/widgets/FirstScreen/first_screen_content.dart';
import 'package:bookapp/stores/home_store.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:bookapp/helpers/constants.dart';
import 'package:bookapp/widgets/common/my_header.dart';
import 'dart:async';


class FirstScreen extends StatefulWidget {
  const FirstScreen({Key? key}) : super(key: key);

  @override
  State<FirstScreen> createState() => _FirstScreenState();
}

class _FirstScreenState extends State<FirstScreen> with AutomaticKeepAliveClientMixin{
  late HomeStore homeStore;
  late EasyRefreshController _controller;

  @override
  void initState(){
    super.initState();
    _controller = EasyRefreshController();
    Future(
        (){
          homeStore = Provider.of<HomeStore>(
            this.context,
            listen: false,
          );
          homeStore.getData();
        }
    );
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return EasyRefresh(
      enableControlFinishRefresh:false,
      bottomBouncing: Theme.of(context).platform == TargetPlatform.iOS,
      header: MyHeader(
          refreshedText: "更新成功",
          refreshFailedText: "刷新失败，请稍后重试",
          refreshReadyText: "松手刷新",
          refreshingText: "正在更新内容",
          refreshText: "下拉刷新",
          textColor: Color(AppColors.fontColorGray),
          showInfo: false,
          alignment: Alignment.bottomCenter,
      ),
      child: ListView(
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(
              left: Constants.pageMargin,
              top:20.0,
              right: Constants.pageMargin,
              bottom: 40.0,
            ),
            child: FirstScreenContent(),
          ),
        ],
      ),
      onRefresh: () async {
        await Future.delayed(
          Duration(seconds:2),
            (){
              _controller.finishRefresh(success:true);
            },
        );
      }
    );
  }

  @override
  bool get wantKeepAlive => true;
}
